var fs = require("fs");
var fm = require("front-matter");

var dir = []; //目录结构
var files = fs.readdirSync('./markdown/');
for (var i = 0; i < files.length; i++) {
    (function(fileName) {
        console.log(fileName);
        var stat = fs.statSync('./markdown/' + fileName);
        if (stat.isDirectory()) {
            console.log(fileName + " is Dir");
        } else if (stat.isFile()) {
            var data = fs.readFileSync("./markdown/" + fileName, 'utf8')
            var md = fm(data);
            md.attributes.time = new Date(stat.atime).toLocaleDateString();
            md.body = md.body.replace(/\.\.\/static\//g, "./static/");
            fs.writeFileSync("./static/art/" + md.attributes.url, md.body, "utf8")

            if (md.attributes.description != "" && md.attributes.description != undefined) {
                dir.push(md.attributes);
            }
        }
    })(files[i])
}
var dirString = "var files = " + JSON.stringify(dir) + ";";
fs.writeFileSync("./static/js/files.js", dirString, "utf8");